function P=compute_constraint_distance_3param_6eq_6unk(m1,m2,m3)

% Copyright (C) <2007>  <Francesc Moreno-Noguer, Vincent Lepetit, Pascal Fua>
% 
% This program is free software: you can redistribute it and/or modify
% it under the terms of the version 3 of the GNU General Public License
% as published by the Free Software Foundation.
% 
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% General Public License for more details.       
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
% Francesc Moreno-Noguer, CVLab-EPFL, September 2007.
% fmorenoguer@gmail.com, http://cvlab.epfl.ch/~fmoreno/ 

%redefine variables name, for compatibility with maple
m1_1=m1(1); 
m1_2=m1(2); 
m1_3=m1(3); 
m1_4=m1(4); 
m1_5=m1(5); 
m1_6=m1(6);
m1_7=m1(7); 
m1_8=m1(8); 
m1_9=m1(9); 
m1_10=m1(10); 
m1_11=m1(11); 
m1_12=m1(12);

m2_1=m2(1); 
m2_2=m2(2); 
m2_3=m2(3); 
m2_4=m2(4); 
m2_5=m2(5); 
m2_6=m2(6);
m2_7=m2(7); 
m2_8=m2(8); 
m2_9=m2(9); 
m2_10=m2(10); 
m2_11=m2(11); 
m2_12=m2(12);

m3_1=m3(1); 
m3_2=m3(2); 
m3_3=m3(3); 
m3_4=m3(4); 
m3_5=m3(5); 
m3_6=m3(6);
m3_7=m3(7); 
m3_8=m3(8); 
m3_9=m3(9); 
m3_10=m3(10); 
m3_11=m3(11); 
m3_12=m3(12);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

t1 = (m1_2 ^ 2);
t4 = (m1_6 ^ 2);
t5 = (m1_3 ^ 2);
t6 = (m1_5 ^ 2);
t11 = (m1_4 ^ 2);
t12 = (m1_1 ^ 2);
t20 = m1_4 * m2_4;
t21 = m1_3 * m2_3;
t22 = m1_5 * m2_5;
t23 = m1_2 * m2_2;
t24 = m1_6 * m2_6;
t25 = m1_1 * m2_1;
t26 = (-m2_4 * m1_1 - m2_5 * m1_2 - m2_6 * m1_3 - m1_6 * m2_3 - m1_4 * m2_1 - m1_5 * m2_2 + t20 + t21 + t22 + t23 + t24 + t25);
t27 = m1_6 * m3_6;
t29 = m1_5 * m3_5;
t30 = m1_4 * m3_4;
t33 = m1_3 * m3_3;
t35 = m1_1 * m3_1;
t38 = m1_2 * m3_2;
t39 = (t27 - m1_6 * m3_3 + t29 + t30 - m1_4 * m3_1 - m3_6 * m1_3 + t33 - m3_5 * m1_2 + t35 - m3_4 * m1_1 - m1_5 * m3_2 + t38);
t40 = (m2_4 ^ 2);
t41 = (m2_2 ^ 2);
t42 = (m2_5 ^ 2);
t43 = (m2_1 ^ 2);
t44 = (m2_6 ^ 2);
t45 = (m2_3 ^ 2);
t53 = m2_4 * m3_4;
t56 = m2_5 * m3_5;
t57 = m2_2 * m3_2;
t60 = m2_1 * m3_1;
t62 = m2_6 * m3_6;
t63 = m2_3 * m3_3;
t65 = (t53 - m2_4 * m3_1 - m3_4 * m2_1 + t56 + t57 - m2_5 * m3_2 - m2_6 * m3_3 + t60 - m3_6 * m2_3 + t62 + t63 - m3_5 * m2_2);
t66 = (m3_5 ^ 2);
t69 = (m3_4 ^ 2);
t70 = (m3_3 ^ 2);
t71 = (m3_2 ^ 2);
t72 = (m3_6 ^ 2);
t75 = (m3_1 ^ 2);
t81 = (m1_9 ^ 2);
t82 = (m1_8 ^ 2);
t83 = (m1_7 ^ 2);
t90 = m1_8 * m2_8;
t91 = m1_7 * m2_7;
t93 = m1_9 * m2_9;
t98 = (-m1_9 * m2_3 + t21 + t90 + t23 + t91 + t25 - m2_7 * m1_1 + t93 - m2_9 * m1_3 - m1_8 * m2_2 - m1_7 * m2_1 - m2_8 * m1_2);
t101 = m1_9 * m3_9;
t102 = m1_7 * m3_7;
t106 = m1_8 * m3_8;
t108 = (-m3_8 * m1_2 + t38 + t33 - m3_9 * m1_3 + t101 + t102 - m1_7 * m3_1 - m1_9 * m3_3 + t35 - m3_7 * m1_1 + t106 - m1_8 * m3_2);
t113 = (m2_8 ^ 2);
t116 = (m2_9 ^ 2);
t117 = (m2_7 ^ 2);
t119 = m2_8 * m3_8;
t122 = m2_9 * m3_9;
t125 = m2_7 * m3_7;
t128 = (t119 - m2_7 * m3_1 - m3_8 * m2_2 + t57 + t63 + t60 + t122 - m2_8 * m3_2 - m3_7 * m2_1 + t125 - m3_9 * m2_3 - m2_9 * m3_3);
t129 = (m3_7 ^ 2);
t130 = (m3_8 ^ 2);
t133 = (m3_9 ^ 2);
t141 = (m1_10 ^ 2);
t142 = (m1_11 ^ 2);
t143 = (m1_12 ^ 2);
t151 = m1_10 * m2_10;
t152 = m1_12 * m2_12;
t154 = m1_11 * m2_11;
t158 = (-m2_10 * m1_1 - m2_12 * m1_3 + t151 + t23 + t25 + t152 + t21 - m1_10 * m2_1 + t154 - m1_11 * m2_2 - m2_11 * m1_2 - m1_12 * m2_3);
t160 = m1_12 * m3_12;
t164 = m1_10 * m3_10;
t165 = m1_11 * m3_11;
t168 = (-m3_10 * m1_1 + t160 - m3_11 * m1_2 + t38 + t33 - m1_12 * m3_3 - m3_12 * m1_3 + t164 + t35 + t165 - m1_11 * m3_2 - m1_10 * m3_1);
t169 = (m2_12 ^ 2);
t170 = (m2_10 ^ 2);
t171 = (m2_11 ^ 2);
t179 = m2_10 * m3_10;
t181 = m2_12 * m3_12;
t185 = m2_11 * m3_11;
t188 = (t57 + t60 + t179 - m2_10 * m3_1 + t181 - m2_12 * m3_3 - m3_12 * m2_3 - m3_10 * m2_1 + t63 + t185 - m2_11 * m3_2 - m3_11 * m2_2);
t191 = (m3_12 ^ 2);
t192 = (m3_10 ^ 2);
t193 = (m3_11 ^ 2);
t212 = (t22 + t91 + t93 - m1_7 * m2_4 + t20 - m2_7 * m1_4 + t90 - m2_8 * m1_5 - m1_8 * m2_5 - m1_9 * m2_6 + t24 - m2_9 * m1_6);
t219 = (t101 + t30 - m3_9 * m1_6 - m3_8 * m1_5 - m1_8 * m3_5 - m3_7 * m1_4 + t27 - m1_9 * m3_6 - m1_7 * m3_4 + t102 + t106 + t29);
t233 = (t53 - m2_8 * m3_5 - m2_9 * m3_6 - m2_7 * m3_4 + t125 + t122 - m3_8 * m2_5 + t62 + t119 + t56 - m3_7 * m2_4 - m3_9 * m2_6);
t254 = (-m2_11 * m1_5 + t154 + t151 - m1_12 * m2_6 + t20 - m1_10 * m2_4 - m2_12 * m1_6 - m2_10 * m1_4 - m1_11 * m2_5 + t152 + t24 + t22);
t261 = (t30 - m3_12 * m1_6 - m1_10 * m3_4 - m1_11 * m3_5 + t160 + t27 + t164 + t165 - m3_11 * m1_5 - m3_10 * m1_4 - m1_12 * m3_6 + t29);
t275 = (-m3_10 * m2_4 + t56 - m2_10 * m3_4 + t62 - m3_12 * m2_6 - m2_11 * m3_5 + t53 - m3_11 * m2_5 - m2_12 * m3_6 + t179 + t181 + t185);
t296 = (-m2_12 * m1_9 + t152 + t91 + t90 - m1_11 * m2_8 + t151 - m2_11 * m1_8 + t93 - m1_10 * m2_7 - m1_12 * m2_9 - m2_10 * m1_7 + t154);
t303 = (t164 + t102 - m3_10 * m1_7 + t160 - m3_12 * m1_9 - m1_10 * m3_7 - m3_11 * m1_8 + t101 - m1_12 * m3_9 + t106 + t165 - m1_11 * m3_8);
t317 = (t125 + t119 - m3_12 * m2_9 - m3_10 * m2_7 - m2_11 * m3_8 - m3_11 * m2_8 + t122 + t179 - m2_12 * m3_9 + t181 - m2_10 * m3_7 + t185);
P(1,1) = t1 - 2 * m1_4 * m1_1 + t4 + t5 + t6 - 2 * m1_5 * m1_2 - 2 * m1_6 * m1_3 + t11 + t12;
P(1,2) = 2 * t26;
P(1,3) = 2 * t39;
P(1,4) = t40 + t41 + t42 + t43 + t44 + t45 - 2 * m2_5 * m2_2 - 2 * m2_6 * m2_3 - 2 * m2_4 * m2_1;
P(1,5) = 2 * t65;
P(1,6) = t66 - 2 * m3_4 * m3_1 + t69 + t70 + t71 + t72 - 2 * m3_6 * m3_3 + t75 - 2 * m3_5 * m3_2;
P(2,1) = -2 * m1_8 * m1_2 + t12 + t81 + t5 + t82 + t83 + t1 - 2 * m1_9 * m1_3 - 2 * m1_7 * m1_1;
P(2,2) = 2 * t98;
P(2,3) = 2 * t108;
P(2,4) = -2 * m2_8 * m2_2 - 2 * m2_9 * m2_3 + t113 - 2 * m2_7 * m2_1 + t116 + t117 + t41 + t43 + t45;
P(2,5) = 2 * t128;
P(2,6) = t75 + t70 + t129 + t71 + t130 - 2 * m3_9 * m3_3 + t133 - 2 * m3_8 * m3_2 - 2 * m3_7 * m3_1;
P(3,1) = -2 * m1_11 * m1_2 + t141 + t142 + t12 + t1 + t5 + t143 - 2 * m1_10 * m1_1 - 2 * m1_12 * m1_3;
P(3,2) = 2 * t158;
P(3,3) = 2 * t168;
P(3,4) = t169 + t41 + t43 + t45 + t170 + t171 - 2 * m2_12 * m2_3 - 2 * m2_10 * m2_1 - 2 * m2_11 * m2_2;
P(3,5) = 2 * t188;
P(3,6) = t71 - 2 * m3_12 * m3_3 + t75 + t191 + t70 + t192 + t193 - 2 * m3_10 * m3_1 - 2 * m3_11 * m3_2;
P(4,1) = -2 * m1_9 * m1_6 + t11 + t4 + t81 + t82 + t6 + t83 - 2 * m1_7 * m1_4 - 2 * m1_8 * m1_5;
P(4,2) = 2 * t212;
P(4,3) = 2 * t219;
P(4,4) = t117 + t113 - 2 * m2_8 * m2_5 - 2 * m2_7 * m2_4 - 2 * m2_9 * m2_6 + t116 + t40 + t42 + t44;
P(4,5) = 2 * t233;
P(4,6) = t129 - 2 * m3_9 * m3_6 + t69 + t66 + t72 + t133 - 2 * m3_8 * m3_5 - 2 * m3_7 * m3_4 + t130;
P(5,1) = t4 + t143 + t11 - 2 * m1_12 * m1_6 - 2 * m1_11 * m1_5 - 2 * m1_10 * m1_4 + t6 + t141 + t142;
P(5,2) = 2 * t254;
P(5,3) = 2 * t261;
P(5,4) = t170 + t171 + t169 - 2 * m2_10 * m2_4 - 2 * m2_11 * m2_5 - 2 * m2_12 * m2_6 + t40 + t42 + t44;
P(5,5) = 2 * t275;
P(5,6) = t69 + t66 + t72 - 2 * m3_12 * m3_6 - 2 * m3_10 * m3_4 + t193 - 2 * m3_11 * m3_5 + t192 + t191;
P(6,1) = t142 - 2 * m1_10 * m1_7 + t141 + t83 + t143 + t82 - 2 * m1_12 * m1_9 + t81 - 2 * m1_11 * m1_8;
P(6,2) = 2 * t296;
P(6,3) = 2 * t303;
P(6,4) = t171 + t170 - 2 * m2_12 * m2_9 - 2 * m2_10 * m2_7 - 2 * m2_11 * m2_8 + t113 + t169 + t116 + t117;
P(6,5) = 2 * t317;
P(6,6) = -2 * m3_11 * m3_8 + t193 + t133 + t129 + t192 + t130 - 2 * m3_12 * m3_9 + t191 - 2 * m3_10 * m3_7;
